PHP comments function

use PHP jquery json mysql to build comments function

comments

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>comments</title>
<style type="text/css">
.demo{
width:500px;
margin: 0 auto;
}
h3{
font-size:18px
}
#comments{
margin:10px auto
}
#post{
margin-top:10px
}
#comments p,#post p{
line-height:30px
}
#comments p span{
margin:4px; color:#999
}
#message{
position:relative;
display:none;
width:100px;
padding:4px;
margin-top:-100px;
margin-left:30px;
background: #ff0000;
color: #c286ff;
z-index:1001
}
</style>
<script src="http://cdn.bootcss.com/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var comments = $("#comments");
$.getJSON("server.php",function(json){
$.each(json,function(index,array){
var txt = "<p><strong>"+array["user"]+"</strong>:"+array["comment"]+"<span>"+array["addtime"]+"</span></p>";
comments.append(txt);
});
});
$("#add").click(function(){
var user = $("#user").val();
var txt = $("#txt").val();
$.ajax({
type: "POST",
url: "comment.php",
data: "user="+user+"&txt="+txt,
success: function(msg){
if(msg==1){
var str = "<p><strong>"+user+"</strong>:"+txt+"<span>just now</span></p>";
comments.append(str);
$("#message").show().html("go!").fadeOut(1000);
$("#txt").attr("value","");
}else{
$("#message").show().html(msg).fadeOut(1000);
}
}
});
});
});
</script>
</head>
<body>
<div class="demo">
<div id="comments">
<h3>comments list</h3>
</div>
<div id="post">
<h3>comments</h3>
<p>username</p>
<p><input type="text" class="input" id="user" /></p>
<p>contents</p>
<p><textarea class="input" id="txt" style="width:100%; height:80px"></textarea></p>
<p><input type="submit" value="submit" id="add" /></p>
<div id="message"></div>
</div>
</div>
</body>
</html>

create comments database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("Content-type:text/html;charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
// connect
$conn = mysqli_connect($servername, $username, $password);
mysqli_set_charset($conn,'utf8'); //utf-8
// check
if (!$conn) {
die("error " . mysqli_connect_error());
}
// create database
$sql = "CREATE DATABASE comments";
if (mysqli_query($conn, $sql)) {
echo "s";
} else {
echo "e " . mysqli_error($conn);
}
mysqli_close($conn);
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
$sql = "CREATE TABLE `comments` (
`id` int(11) NOT NULL auto_increment,
`user` varchar(30) NOT NULL,
`comment` varchar(200) NOT NULL,
`addtime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM; " ;
if (mysqli_query($conn, $sql)) {
echo "s";
} else {
echo "e " . mysqli_error($conn);
}
mysqli_close($conn);

server.php

1
2
3
4
5
6
7
8
9
10
11
<?php
header("Content-type:text/html;charset=utf-8");
$conn=mysqli_connect("localhost","root","root","comments");
mysqli_set_charset($conn,"utf8");
$sql="SELECT * from comments";
$que=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($que)){
$comments[] = array("id"=>$row[id],"user"=>$row[user],"comment"=>$row[comment],"addtime"=>$row[addtime]);
}
echo json_encode($comments);
?>

comment.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
header("Content-type:text/html;charset=utf-8");
$user = htmlspecialchars(trim($_POST['user']));
$txt = htmlspecialchars(trim($_POST['txt']));
$time = date("Y-m-d H:i:s");
if(empty($user)){
echo "username null";
exit;
}
if(empty($txt)){
echo "contents null";
exit;
}
$conn=mysqli_connect("localhost","root","root","comments");
mysqli_set_charset($conn,"utf8");
$sql="insert into comments(user,comment,addtime)values('$user','$txt','$time')";
$que=mysqli_query($conn,$sql);
if($que) echo "1";
?>